#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#define int long long
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 1005, _mod = 147744151;
array <int, N> mp[N], isl[N];
int mod;
void Sub1(int n, int m) {
	int q = read();
	int _q = 1;
	int _ans = 0;
	while (q--) {
		int _x1 = read(), _y1 = read(), _x2 = read(), _y2 = read();
		int ans = 1;
		for (int i = _x1; i <= _x2; i++)
			for (int j = _y1; j <= _y2; j++)
				ans *= mp[i][j], ans %= mod;
		ans ^= _q;
		_ans += ans % _mod;
		if (_ans >= _mod) _ans -= _mod;
		_q++;
	}
	write(_ans), puts("");
}
int pow_(int x, int k, int p) {
	int ans = 1;
	while (k) {
		if (k & 1) ans = ans * x % p;
		x = x * x % p;
		k >>= 1;
	}
	return ans;
}
void Sub2(int n, int m) {
	for (int i = 0; i <= 1000; i++)
		mp[0][i] = mp[i][0] = isl[0][i] = isl[i][0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			isl[i][j] = isl[i - 1][j] * isl[i][j - 1] % mod * mp[i][j] % mod * mp[i - 1][j - 1] % mod;
			mp[i][j] = pow_(isl[i][j], mod - 2, mod);
		}
	}
	int q = read();
	int _ans = 0, _q = 1;
	while (q--) {
		int _x1 = read(), _y1 = read(), _x2 = read(), _y2 = read();
		int ans = isl[_x2][_y2] * isl[_x1 - 1][_y1 - 1] % mod * mp[_x1 - 1][_y2] % mod * mp[_x2][_y1 - 1] % mod;
		ans ^= _q;
		_ans += ans % _mod;
		if (_ans >= _mod) _ans -= _mod;
		_q++;
	}
	write(_ans), puts("");
}
/*
int calc(int a, int b, int p) {
	return (a % (b * p)) / b;
}
void Sub3(int n, int m) {
	for (int i = 0; i <= 1000; i++)
		mp[0][i] = mp[i][0] = isl[0][i] = isl[i][0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			isl[i][j] = isl[i - 1][j] * isl[i][j - 1] % mod * mp[i][j];
			isl[i][j] = calc(isl[i][j], mp[i - 1][j - 1], mod);
			mp[i][j] = pow_(isl[i][j], mod - 2, mod);
		}
	}
	int q = read();
	int _ans = 0, _q = 1;
	while (q--) {
		int _x1 = read(), _y1 = read(), _x2 = read(), _y2 = read();
		int ans = isl[_x2][_y2] * isl[_x1 - 1][_y1 - 1] % mod * mp[_x1 - 1][_y2] % mod * mp[_x2][_y1 - 1] % mod;
		ans ^= _q;
		_ans += ans % _mod;
		if (_ans >= _mod) _ans -= _mod;
		_q++;
	}
	write(_ans), puts("");
}
int lowbit(int x) {
	return x & -x;
}
void insert(int x, int y, int k, int n, int m, int mod) {
	while (x <= n) {
		int _y = y;
		while (_y <= m) {
			isl[x][_y] *= k;
			isl[x][_y] %= mod;
			_y += lowbit(_y);
		}
		x += lowbit(x);
	}
}
int query(int x, int y, int mod) {
	int ans = 1;
	while (x) {
		int _y = y;
		while (_y) {
			ans *= isl[x][_y];
			ans %= mod;
			_y -= lowbit(_y);
		}
		x -= lowbit(x);
	}
}
void Sub3(int n, int m) {
	for (int i = 0; i <= 1000; i++)
		isl[i].fill(1);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			insert(i, j, mp[i][j], n, m, mod);
	int q = read();
	int _ans = 0, _q = 1;
}
*/
signed main() {
	freopen("tsuki.in", "r", stdin);
	freopen("tsuki.out", "w", stdout);
	int n = read(), m = read();
	mod = read();
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			mp[i][j] = read();
	if (n <= 200 && m <= 200) Sub1(n, m);
	else Sub2(n, m);
	return 0;
}
